package com.android.assignment.taskreminder;

import java.util.Calendar;
import java.util.Date;

import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ActionAfterRemindFragment extends Fragment implements LoaderCallbacks<Cursor>{
	public static final String ACTION_REMIND_TAG = "actionremind";
	
	private TextView remindtitle;
	private TextView remindbody;
	private Button donebutton;
	private Button dismissbutton;
	private TextView laterbutton;
	private RadioButton latertime;
	private RadioGroup latertimegroup;
	private long Rowid;
	private Calendar myCalendar;
	private Calendar myCalendarforloop;
	
	//private String title;
	//private String body;
	//private long datetime;
	//private Date date;
	//private long monthmilis = 
	private String status;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if(args!=null)
			Rowid = args.getLong(TaskReminderProvider.COLUMN_ROWID);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View v = inflater.inflate(R.layout.action_after_remind_fragment_view, container, false);
		remindtitle = (TextView) v.findViewById(R.id.taskname);
		remindbody = (TextView) v.findViewById(R.id.tasknote);
		
		donebutton = (Button) v.findViewById(R.id.done);
		donebutton.setBackgroundResource(R.drawable.done);
		
		laterbutton = (TextView) v.findViewById(R.id.nooze);
		laterbutton.setBackgroundResource(R.drawable.plus);
		
		dismissbutton = (Button) v.findViewById(R.id.dismiss);
		dismissbutton.setBackgroundResource(R.drawable.cross);
		
		latertimegroup = (RadioGroup) v.findViewById(R.id.noozegroup);
		myCalendar = Calendar.getInstance();
		myCalendarforloop = Calendar.getInstance();
		laterbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int id = latertimegroup.getCheckedRadioButtonId();
				//latertime = (RadioButton) v.findViewById(id);
				//String text = latertime.getText().toString();
				switch(id){
					case R.id.fifteenmns:
					myCalendar.add(Calendar.MINUTE, Integer.parseInt("15"));
					Toast.makeText(getActivity(), "I'll remind you 15' later", Toast.LENGTH_SHORT).show();
					break;
					case R.id.thirtymns:
					myCalendar.add(Calendar.MINUTE, Integer.parseInt("30"));
					Toast.makeText(getActivity(), "Don't forget your job", Toast.LENGTH_SHORT).show();
					break;
					case R.id.sixtymns:
					myCalendar.add(Calendar.MINUTE, Integer.parseInt("60"));
					Toast.makeText(getActivity(), "Never say die ?!!", Toast.LENGTH_SHORT).show();
					break;
				}
				getActivity().finish();
				new ReminderManager(getActivity()).setReminder(Rowid, myCalendar);
			}
		});

		dismissbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "Never give up!", Toast.LENGTH_SHORT).show();
				
				if(status.equals("Only Once")){
					getActivity().getContentResolver().delete(ContentUris.withAppendedId(
							TaskReminderProvider.CONTENT_URI, Rowid), null, null);
					//Toast.makeText(getActivity(), "here is ++ " + status, Toast.LENGTH_SHORT).show();
				}else{
					ContentValues values = new ContentValues();
					if(status.equals("Once a Day")){
						myCalendarforloop.add(Calendar.HOUR, 24);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Day ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Week")){
						myCalendarforloop.add(Calendar.DATE, 7);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Week ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Month")){
						myCalendarforloop.add(Calendar.MONTH, 1);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Month ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Year")){
						myCalendarforloop.add(Calendar.YEAR, 1);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Year ++ " + status, Toast.LENGTH_SHORT).show();
					}
					values.put(TaskReminderProvider.COLUMN_ROWID, Rowid);
					values.put(TaskReminderProvider.COLUMN_TITLE, remindtitle.getText().toString());
					values.put(TaskReminderProvider.COLUMN_CONTENT, remindbody.getText().toString());
					values.put(TaskReminderProvider.COLUMN_DATE_TIME, myCalendarforloop.getTimeInMillis());
					
						int count = getActivity().getContentResolver().update(ContentUris.withAppendedId(
										TaskReminderProvider.CONTENT_URI, Rowid),values, null, null);
						Toast.makeText(getActivity(), " Here I'll remind you at "
								+ myCalendarforloop.getTime(), Toast.LENGTH_SHORT).show();
						if (count == -1)
							throw new IllegalStateException("Unable to update "+ Rowid);
				}
					getActivity().finish();
			}
		});
		
		donebutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "Congratulation! You've done your task", Toast.LENGTH_SHORT).show();
				if(status.equals("Only Once")){
					getActivity().getContentResolver().delete(ContentUris.withAppendedId(
							TaskReminderProvider.CONTENT_URI, Rowid), null, null);
					//Toast.makeText(getActivity(), "here is ++ " + status, Toast.LENGTH_SHORT).show();
				}else{
					ContentValues values = new ContentValues();
					if(status.equals("Once a Day")){
						myCalendarforloop.add(Calendar.HOUR, 24);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Day ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Week")){
						myCalendarforloop.add(Calendar.DATE, 7);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Week ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Month")){
						myCalendarforloop.add(Calendar.MONTH, 1);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Month ++ " + status, Toast.LENGTH_SHORT).show();
					}else if(status.equals("Once a Year")){
						myCalendarforloop.add(Calendar.YEAR, 1);
						values.put(TaskReminderProvider.COLUMN_STATUS, status);
						//Toast.makeText(getActivity(), "here is Once a Year ++ " + status, Toast.LENGTH_SHORT).show();
					}
					values.put(TaskReminderProvider.COLUMN_ROWID, Rowid);
					values.put(TaskReminderProvider.COLUMN_TITLE, remindtitle.getText().toString());
					values.put(TaskReminderProvider.COLUMN_CONTENT, remindbody.getText().toString());
					values.put(TaskReminderProvider.COLUMN_DATE_TIME, myCalendarforloop.getTimeInMillis());
					
						int count = getActivity().getContentResolver().update(ContentUris.withAppendedId(
										TaskReminderProvider.CONTENT_URI, Rowid),values, null, null);
						Toast.makeText(getActivity(), " Here I'll remind you at "
								+ myCalendarforloop.getTime(), Toast.LENGTH_SHORT).show();
						if (count == -1)
							throw new IllegalStateException("Unable to update "+ Rowid);
				}
				getActivity().finish();
			}
		});
		getLoaderManager().initLoader(0, null, this);
		return v;
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int arg0,
			Bundle arg1) {
		// TODO Auto-generated method stub
		return new CursorLoader(getActivity(),ContentUris.withAppendedId(
				TaskReminderProvider.CONTENT_URI, Rowid),null,null,null,null);
	}
	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		if(arg1.getCount()==0){
			new Handler().post(new Runnable(){
				@Override
				public void run() {
					// TODO Auto-generated method stub
					getActivity().finish();
				}			
			});
			return;
		}
		remindtitle.setText(arg1.getString(arg1
				.getColumnIndexOrThrow(TaskReminderProvider.COLUMN_TITLE)));
		remindbody.setText(arg1.getString(arg1
				.getColumnIndexOrThrow(TaskReminderProvider.COLUMN_CONTENT)));
		status = arg1.getString(arg1.getColumnIndexOrThrow(TaskReminderProvider.COLUMN_STATUS));
		Long datetime = arg1.getLong(arg1
				.getColumnIndexOrThrow(TaskReminderProvider.COLUMN_DATE_TIME));
		Date date = new Date(datetime);
		myCalendarforloop.setTime(date);
	}
	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	

}
